package com.example.first.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.first.R;
import com.example.first.modle.ApiResponse;
import com.example.first.modle.Register;
import com.example.first.services.ApiService;
import com.example.first.utils.RetrofitClient;

import retrofit2.Call;

public class RegisterActivity extends AppCompatActivity {


    private EditText etEmail;
    private EditText etPassword;
    private EditText etCaptcha;
    private Button btnSendCaptcha;
    private Button btnRegister;
    private Button btnBackToLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


        etEmail = findViewById(R.id.et_email);
        etPassword = findViewById(R.id.et_password);
        etCaptcha = findViewById(R.id.et_captcha);
        btnSendCaptcha = findViewById(R.id.btn_send_captcha);
        btnRegister = findViewById(R.id.btn_register);
        btnBackToLogin = findViewById(R.id.btn_back_to_login);

        //发送验证码按钮的点击事件
        btnSendCaptcha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断邮箱格式是否正确
                String email = etEmail.getText().toString().trim();

                if (email.isEmpty()) {
                    Log.e("RegisterActivity", "Email is empty");
                    Toast.makeText(RegisterActivity.this, "请输入邮箱地址", Toast.LENGTH_SHORT).show();
                    return;
                }

                String regex = "^[A-Za-z0-9+_.-]+@(.+)$";
                if (!email.matches(regex)) {
                    Toast.makeText(RegisterActivity.this, "请输入正确的邮箱格式", Toast.LENGTH_SHORT).show();
                    return;
                }


                //发送验证码的逻辑
                ApiService apiService = RetrofitClient.getApiService();
                Call<ApiResponse<String>> call=apiService.sendMsg(etEmail.getText().toString());
                call.enqueue(new retrofit2.Callback<ApiResponse<String>>() {
                    @Override
                    public void onResponse(Call<ApiResponse<String>> call, retrofit2.Response<ApiResponse<String>> response) {
                        if(response.isSuccessful() && response.body() != null){
                            //成功发送请求

                        }else {
                            Toast.makeText(RegisterActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                        //处理失败情况
                        t.printStackTrace();
                    }
                });

                Toast.makeText(RegisterActivity.this, "验证码已发送", Toast.LENGTH_SHORT).show();

                // 60秒倒计时
                new CountDownTimer(60000, 1000) {
                    public void onTick(long millisUntilFinished) {
                        btnSendCaptcha.setText("重新发送 (" + millisUntilFinished / 1000 + ")");
                        btnSendCaptcha.setEnabled(false);
                    }

                    public void onFinish() {
                        btnSendCaptcha.setText("发送验证码");
                        btnSendCaptcha.setEnabled(true);
                    }
                }.start();
            }
        });

        //注册按钮的点击事件
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String email = etEmail.getText().toString();
                String password = etPassword.getText().toString();
                String enteredCaptcha = etCaptcha.getText().toString();

                if ( email.isEmpty() || password.isEmpty() || enteredCaptcha.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "所有字段都不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    // 注册逻辑
                    Register register =new Register();
                    //验证码
                    register.setCode(Integer.valueOf(etCaptcha.getText().toString().trim()));
                    //邮箱
                    register.setEmail(etEmail.getText().toString().trim());
                    //密码
                    register.setPassword(etPassword.getText().toString().trim());

                    ApiService apiService = RetrofitClient.getApiService();
                    Call<ApiResponse<String>> call=apiService.register(register);
                    call.enqueue(new retrofit2.Callback<ApiResponse<String>>() {
                        @Override
                        public void onResponse(Call<ApiResponse<String>> call, retrofit2.Response<ApiResponse<String>> response) {
                            if(response.isSuccessful() && response.body() != null){
                                //成功发送请求
                                ApiResponse<String> body = response.body();
                                if(body.getCode()==1){
                                    //成功注册
                                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                    finish();
                                }else {
                                    //失败
                                    etPassword.setText("");
                                    etCaptcha.setText("");
                                    Toast.makeText(RegisterActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                                }

                            }else {
                                Toast.makeText(RegisterActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                            //处理失败情况
                            t.printStackTrace();
                        }
                    });



                }
            }
        });

        //返回登录按钮的点击事件
        btnBackToLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });
    }
}
